package com.funny.learning.english.activity;

import java.util.Random;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.funny.learning.english.GameBaseActivity;
import com.funny.learning.english.R;
import com.funny.learning.english.model.GameEntity;
import com.funny.learning.english.model.HistoryEntity;

@SuppressLint("SimpleDateFormat")
public class ChonTuActivity extends GameBaseActivity implements OnClickListener {
	
	private String TAG = "ChonTuActivity";

	private TextView tv1, tv2;


	private int screenTrue = 0;

	private String str1, str2;

	private String strDapAn;

	private ImageView ivDes;

	private TextView tvTitle, tvTiengViet;
	
	private boolean isPause;

	private ImageView ivPause;

	private ImageView ivPlay;

	private ImageView ivRefresh;

	private ImageView ivBack;

	private LinearLayout llPause;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) { 
		setContentView(R.layout.chon_tu_layout);
		super.onCreate(savedInstanceState);
		ivPause = (ImageView)findViewById(R.id.ivPause);
		ivPlay = (ImageView)findViewById(R.id.ivPlay);
		ivRefresh = (ImageView)findViewById(R.id.ivRefresh);
		ivBack = (ImageView)findViewById(R.id.ivBack);
		llPause = (LinearLayout)findViewById(R.id.llPause);
		tvTitle = (TextView)findViewById(R.id.tvTitle);
		ivBack.setOnClickListener(this);
		ivPause.setOnClickListener(this);
		ivPlay.setOnClickListener(this);
		ivRefresh.setOnClickListener(this);
	}

	private void setAnimation(final Animation animation, final View view) {
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View arg01) {

				switch (arg01.getId()) {
				case R.id.tv1:
					if (tv1.getText().toString().equals(strDapAn)) {
						view.setBackgroundResource(R.drawable.chon_tu_dung);
					} else {
						view.setBackgroundResource(R.drawable.chon_tu_sai);
					}
					break;
				case R.id.tv2:
					if (tv2.getText().toString().equals(strDapAn)) {
						view.setBackgroundResource(R.drawable.chon_tu_dung);
					} else {
						view.setBackgroundResource(R.drawable.chon_tu_sai);
					}
					break;
				default:
					break;
				}

				view.startAnimation(animation);

				animation.setAnimationListener(new AnimationListener() {

					@Override
					public void onAnimationStart(Animation arg0) {
						// Toast.makeText(getBaseContext(), "Starrt Animation",
						// 1000).show();
					}

					@Override
					public void onAnimationRepeat(Animation arg0) {

					}

					@Override
					public void onAnimationEnd(Animation arg0) {
						switch (arg01.getId()) {
						case R.id.tv1:
							if (tv1.getText().toString().equals(strDapAn)) {
								if(countCheckScreen < screenTrue){
									countCheckScreen = countCheckScreen + 1;
									countAnwserCorrect = countAnwserCorrect + 1;
								}
								screenTrue = screenTrue + 1;
								if(screenTrue >= mGameEntities.size()){
									finishGame();
									HistoryEntity historyEntity = new HistoryEntity(1, thumbTopic, slugGameName, countAnwserCorrect, mGameEntities.size(), (int) System.currentTimeMillis(), slugTopicName, slugSubTopicName);
									mHistoryDAO.saveHistory(historyEntity);
									Log.d(TAG, "Tra loi dung:" + countAnwserCorrect + "/" + mGameEntities.size());
								}else{
									initData(screenTrue);
								}
							} else {
								if(countCheckScreen < screenTrue){
									countCheckScreen = countCheckScreen + 1;
								}
								view.setBackgroundResource(R.drawable.stroke_img_topic_chon_tu);
							}
							break;
						case R.id.tv2:
							if (tv2.getText().toString().equals(strDapAn)) {
								if(countCheckScreen < screenTrue){
									countCheckScreen = countCheckScreen + 1;
									countAnwserCorrect = countAnwserCorrect + 1;
								}
								screenTrue = screenTrue + 1;
								if(screenTrue >= mGameEntities.size()){
									finishGame();
									HistoryEntity historyEntity = new HistoryEntity(1, thumbTopic, slugGame, countAnwserCorrect, mGameEntities.size(), (int) System.currentTimeMillis(), slugTopic, slugSubTopic);
									mHistoryDAO.saveHistory(historyEntity);
									Log.d(TAG, "Tra loi dung:" + countAnwserCorrect + "/" + mGameEntities.size());
								}else{
									initData(screenTrue);
								}
								
							} else {
								if(countCheckScreen < screenTrue){
									countCheckScreen = countCheckScreen + 1;
								}
								view.setBackgroundResource(R.drawable.stroke_img_topic_chon_tu);
							}
							break;
						default:
							break;
						}
					}
				});
			}
		});
	}

	@Override
	protected void initSucess() {
		// TODO Auto-generated method stub
		super.initSucess();
		intAB("Chọn từ");

		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);
		ivDes = (ImageView) findViewById(R.id.ivDes);
		tvTiengViet = (TextView) findViewById(R.id.tvTiengViet);
		// data đã dc load hết
		Toast.makeText(activity, "size " + mGameEntities.size(), 2000).show();
		
		initData(screenTrue);
		
	}
	
	private void initData(int count){
		
		GameEntity gameEntity = mGameEntities.get(count);
		Bitmap bm1 = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(getPathImg(mGameEntities.get(count))),getResources().getDimensionPixelSize(R.dimen.game_img_big), getResources().getDimensionPixelSize(R.dimen.game_img_big), true);
		ivDes.setImageBitmap(bm1);
		tvTiengViet.setText(gameEntity.getName());
		
		strDapAn = gameEntity.getLname();
		str1 = gameEntity.getLname();
		
		if(str1 == null){
			screenTrue = screenTrue + 1;
			if(screenTrue >= mGameEntities.size()){
				finishGame();
			}else{
				initData(screenTrue);
			}
			
		}else{
			getRandomGameEntity();
			if(getNumberRandom() == 1){
				Log.d(TAG, "value Random: 1");
				tv1.setText(str1);
				tv2.setText(str2);
			}else{
				Log.d(TAG, "value Random: 2");
				tv1.setText(str2);
				tv2.setText(str1);
			}
			refreshData();
		}
	}
	
	

	private void refreshData() {
		
		tv1.setBackgroundResource(R.drawable.stroke_img_topic_chon_tu);
		tv2.setBackgroundResource(R.drawable.stroke_img_topic_chon_tu);

		Animation animation1 = AnimationUtils.loadAnimation(
				ChonTuActivity.this, R.anim.g_mw_wrong);
		Animation animation2 = AnimationUtils.loadAnimation(
				ChonTuActivity.this, R.anim.g_mw_wrong);
		setAnimation(animation1, tv1);
		setAnimation(animation2, tv2);
	}
	
	private int getNumberRandom(){
		Random ran = new Random();
		int x = ran.nextInt(2);
		
		Log.d(TAG, "number:" + x);
		
		return x;
	}
	
	private void getRandomGameEntity(){
		str2 = mGameEntities.get(new Random().nextInt(mGameEntities.size())).getLname();
		if(str2 == null){
			getRandomGameEntity();
		}else{
			if(str2.equals(strDapAn)){
				getRandomGameEntity();
			}
		}
		
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.ivPause:
			isPause = !isPause;
			if(isPause){
				ivPause.setImageResource(R.drawable.ic_play);
				llPause.setVisibility(View.VISIBLE);
			}else{
				ivPause.setImageResource(R.drawable.ic_pause);
				llPause.setVisibility(View.GONE);
			}
			break;
		case R.id.ivPlay:
			ivPause.setImageResource(R.drawable.ic_pause);
			llPause.setVisibility(View.GONE);
			break;
		case R.id.ivRefresh:
			ivPause.setImageResource(R.drawable.ic_pause);
			llPause.setVisibility(View.GONE);
			screenTrue = 0;
			initData(screenTrue);
			break;
		case R.id.ivBack:
			onBackPressed();
			break;

		default:
			break;
		}
	}

}
